<?php
namespace app\common\service;


use think\Db;
use think\Model;

class PaymentService extends BaseService
{

    /**
     * 后台: 微信支付配置
     * @param $sid
     * @param $wx_pay_mchid
     * @param $wx_pay_key
     * @return int|string
     * @throws \think\Exception
     */
    public function paymentSetting($sid, $wx_pay_mchid, $wx_pay_key){
        $payment_config = [
            'wx_pay_mchid' => $wx_pay_mchid,
            'wx_pay_key' => $wx_pay_key
        ];

        $info = db('payment')->where(['sid' => $sid, 'is_wx' => 1, 'status' => 1])->find();
        if($info){
            //更新
            $data = [
                'payment_config' => json_encode($payment_config),
            ];
            $r = db('payment')->where(['sid' => $sid, 'is_wx' => 1, 'status' => 1])->update($data);
        } else {
            //新增
            $data = [
                'sid' => $sid,
                'payment_code' => 'wxpay',
                'payment_name' => '微信支付',
                'payment_desc' => '微信支付',
                'payment_config' => json_encode($payment_config),
                'is_wx' => 1,
                'status' => 1
            ];
            $r = db('payment')->insert($data);
        }
        return $r;
    }

    /**
     * 后台: 获取微信支付配置详情
     * @param $sid
     * @return array|mixed
     */
    public function getPaymentConfig($sid){
        $payment_config = db('payment')->where(['sid' => $sid, 'is_wx' => 1, 'status' => 1])->value('payment_config');
        $data = [];
        if($payment_config){
            $data = json_decode($payment_config, 1);
        }
        return $data;
    }
}